package com.r7data.dbstruct.config;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DbSource {
	
	/**
	 * 或得数据库连接
	 * @param dbUrl
	 * @param dbUser
	 * @param dbPassword
	 * @return
	 * @throws Exception
	 */
	public static Connection getCon(String driverClass, String dbUrl, String dbUser, String dbPassword) throws Exception{
		
		Class.forName(driverClass);
		
		Connection con = DriverManager.getConnection(dbUrl, dbUser, dbPassword);

		return con;
	}
	
	/**
	 * 关闭数据库
	 * @param con
	 * @param pst
	 * @param rst
	 */
	public static void closeDb(Connection con, PreparedStatement pst, ResultSet rst){
		if(rst != null){
			try {
				rst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(pst != null){
			try {
				pst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con != null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 关闭数据库
	 * @param con
	 * @param stmt
	 * @param rst
	 */
	public static void closeDb(Connection con, Statement stmt, ResultSet rst){
		if(rst != null){
			try {
				rst.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(con != null){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
